package net.gazhi.delonix.rbac.entity;

import java.util.Set;
import java.util.TreeSet;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

/**
 * 子模块<br>
 * 对应 Spring MVC 的控制器类
 * 
 * @author Jeffrey Lin
 *
 */
@Entity(name = DX_TABLE.dx_controller)
public class Controller implements Comparable<Controller> {

	@Id
	private int id;

	@Column
	private String name;

	@Column(name = "class_name")
	private String className;

	@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
	@JoinColumn(name = "module_id", nullable = false)
	private Module module;

	/**
	 * 非持久化集合，用于输出计算
	 */
	@Transient
	private TreeSet<Action> actions = new TreeSet<Action>();

	public Controller() {
	}

	public Controller(int id, String name, String className, Module module) {
		this.id = id;
		this.name = name;
		this.className = className;
		this.module = module;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public Module getModule() {
		return module;
	}

	public void setModule(Module module) {
		this.module = module;
	}

	/**
	 * 非持久化集合，用于输出计算
	 */
	public Set<Action> getActions() {
		return actions;
	}

	@Override
	public int compareTo(Controller target) {
		int i = this.getModule().compareTo(target.getModule());
		if (i == 0) {
			i = Integer.compare(this.id, target.id);
		}
		return i;
	}

}
